gallery.admin = liwe.module ( "gallery.admin", null, "Gallery" );

gallery.admin._dest_div = 'system-content';
gallery.admin._cur_id_gal = 0;
gallery.admin._cur_gal_name = '';

gallery.admin._set_dest_div = function ( dest_div )
{
	gallery.admin._dest_div =  dest_div;
};

gallery.admin._init_admin  = function ()
{
	var _acts = [
		/*	{ label: 'Lista elementi', func: function () { gallery.admin._render (); gallery.admin.list ( 'SC-admin-man' ); } }, 
			{ label: 'Aggiungi', func: function () { gallery.admin.add_item (); } },
		*/
			{ label: 'Gestione', func: function () { gallery.admin._render_sc (); } }
		    ];

        var _plugin = [
                {
                        label:   "Inserisci gallery",
                        handler: gallery.admin._jconed_handler,
                        context: gallery.admin._jconed_context
                }
        ];

	gallery.admin.register ( _acts, _plugin );
	gallery.admin._am_gallery_init ( 'ga-workspace' );	
	gallery.admin._am_gal_init ( 'ga-gal-list' );


} ;

gallery.admin.init = function ( dest_div )
{
	//if ( ! dest_div ) gallery.admin._set_dest_div ( gallery.admin._dest_div );
	if ( dest_div ) gallery.admin._set_dest_div ( dest_div );

	// no longer supported gallery.admin._get_tags_list ();

	if ( ! gallery.templates ) 
	{
		gallery.load_templates ( gallery.admin._init_admin );
		return;
	} 
	gallery.admin._init_admin ();
};

gallery.admin._render_panel = function ( panel, title,  cback )
{
        var dct = { '_panel' : gallery.templates [ panel ], '_title' : title };
        $( gallery.admin._dest_div  , String.formatDict ( gallery.templates [ 'GA-ADMIN-LAYOUT' ], dct ) );
        if ( cback ) cback();
};

gallery.admin._render_sc = function ( dest_div )
{
        gallery.admin._render_panel ( 'GA-ADMIN-GALLERY', 'Gestione Gallery', function ()
        {
		gallery.admin.am_gal.show_list( dest_div);
		gallery.admin.add_gal();
        });
};

//{{{ Gestione Configurazioni

//{{{ ADMIN MANAGER CONF
gallery.admin._am_gal_init = function ( dest_div )
{
        if ( gallery.admin.am_gal ) return;
        gallery.admin.am_gal = new AdminManager
                                ( {
                                        'dest_res'    : dest_div,
                                        'name'          : 'am_gal',
                                        'list_action' : 'gallery.ajax.list_galleries',
                                        'edit_action' : 'gallery.admin.edit_gal',
                                        'del_action' : 'gallery.admin.del_gal',
                                        'mod_menu_actions': [
                                                                 { func: "gallery.admin.add_gal()", 'class' : 'gal_new' , label: 'Nuova Gallery' }
                                                        ]
                                } );
        
        
        gallery.admin.am_gal.set_row_fields (
                                [
                                        [ "Nome", "name" ],
					[ "L", "w" ],
					[ "H", "h"],
					[ "Playtime", "playtime" ]
                                ]
                                );

        gallery.admin.am_gal.set_row_actions ( [
                                                        [ gallery.templates [ 'GA-ADMIN-LIST-ACTION' ], "list_images" ]
                                                ] );
        gallery.admin.am_gal.cbacks [ 'row_manip' ] = gallery.admin._rwman_gal;
        gallery.admin.am_gal.cbacks [ 'edit_item' ] = function ( id ) { gallery.admin.edit_gal ( id ); };
        gallery.admin.am_gal.cbacks [ 'del_item' ] = function ( id ) { gallery.admin.del_gal ( id ); };
        gallery.admin.am_gal.cbacks [ 'list_images' ] = function ( id ) { gallery.admin.list_images ( id ); };
        gallery.admin.am_gal.cbacks [ 'pre_del' ] = gallery.admin._cat_pre_del;

        gallery.admin.am_gal.templates [ 'module_single_action' ] = gallery.templates [ 'module_single_action' ];
        gallery.admin.am_gal.templates [ 'actions_row_action' ] = gallery.templates [ 'ga_gal_actions_row_action' ];
};

gallery.admin._rwman_gal = function ( ds, row )
{
        row [ '_id' ] = row [ 'id' ];
};

//}}}

//{{{ Gallery.admin._confgi_form
gallery.admin._gallery_form = function ()
{
        var f = new liwe.form.instance ( 'gallery_form' );
        f.hidden ( 'iid' , 0 );
        f.text ( { 'name' : 'name', 'size' : 20, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Nome' } );
	if ( gallery.admin._cur_id_gal == 0 )
	{
		f.label ( { 'value' : 'Dimensioni icone' } );
        	f.number ( { 'name' : 'w', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Larghezza ( W )' } );
	        f.number ( { 'name' : 'h', 'size' : 5, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Altezza ( H )' } );
	};
        f.button ( { value: 'Salva', onclick : "gallery.admin._save_gal()", 'class' : 'ga-save-btn' } );
        f.set ( 'ga-workspace' );
};
//}}}

//{{{ Configurazioni ADD EDIT SAVE DELETE
gallery.admin.add_gal = function ( dest )
{
	gallery.admin._cur_id_gal = 0;
	gallery.admin._cur_gal_name = '';
        $ ( 'title-work', 'Nuova Configurazione' );
        gallery.admin._gallery_form();
};

gallery.admin.edit_gal = function ( id_gallery )
{
        $ ( 'title-work', 'Modifica Gallery' ); 
        gallery.admin._cur_id_gal = id_gallery ;
        gallery.admin._gallery_form();
        var v = { action : 'gallery.ajax.get_gallery', iid : id_gallery };
        gallery.ajax ( v , function ( data )
        {
		if ( ! data.get ( 'gallery' ) ) return;
                gallery.admin._cur_gal_name = data [ 'gallery' ].get ( 'name' );
                var f = liwe.form.get ( 'gallery_form' );
                data [ 'gallery' ].iterate ( function ( val, k )
                {
                        f.set_value ( k, val );
                });
                
        });
};

gallery.admin._save_gal = function ()
{
        var cback = gallery.admin._render_sc;
        var msg = 'Configurazione Salvata';
        var id_gal = gallery.admin._cur_id_gal;
        gallery.admin._cur_id_gal = 0;


        var f = liwe.form.get ( 'gallery_form' );
        if ( ! f.check () )
        {
		if ( typeof ( system.admin.do_alert ) == 'undefined') { alert ( 'Inserire tutti i valori' ); return }
                system.admin.do_alert ( 'Inserire tutti i valori', liwe.utils.notifier.mode.WARN );
                return;
        }

        var dct = f.get_values ();

        dct [ 'action' ] = "gallery.admin.add_gal";

	gallery.admin.ajax ( dct, function ( v ) 
	{
		if ( typeof ( system.admin.do_alert ) == 'undefined') { alert ( msg ); return }
                system.admin.do_alert ( msg, liwe.utils.notifier.mode.NOTICE );
                cback();
        } );

};

gallery.admin.del_gal = function ( ids )
{
	var gal = false;
	conf = confirm ( "Vuoi eliminare l'elemento ?" );
	if ( conf )
	{
		gallery.admin.ajax ( { action : "gallery.admin.del_gal", ids : ids }, function ( data )
					{
						gallery.admin._render_sc();
					} );
	}
};

//}}}

gallery.admin.list_images = function ( id_gal )
{
        var v = { action : 'gallery.ajax.get_gallery', iid : id_gal };
        gallery.ajax ( v , function ( data )
        {
                gallery.admin._cur_gal_name = data [ 'gallery' ] [ 'name' ];
                $( 'title-work' , 'Lista Immagini per - [ ' + gallery.admin._cur_gal_name + ' ]' );
                gallery.admin._cur_id_gal = id_gal ;
                gallery.admin.am_gallery.show_list ( 'ga-workspace' );
        });

	
};

//}}}

//{{{ Gestione Immagini collegate alla configurazione

//{{{ Admin Manager Immagini
gallery.admin._am_gallery_init = function ( dest_div )
{
        if ( gallery.admin.am_gallery ) return;
        gallery.admin.am_gallery = new AdminManager
                                ( {
                                        'dest_res': dest_div,
					'name' : 'am_gallery',
                                        'list_action': "gallery.admin.list_imgs",
                                        'del_action':  "gallery.admin.delete",
                                        'add_action':  "gallery.admin.add_item",
                                        'edit_action':  "gallery.admin.edit",
                                        'mod_menu_actions': [
                                                                 { func: "gallery.admin.add_item()", 'class' : 'gal_new' , label: 'Aggiungi Immagine' }
],
					'filter_form' : gallery.admin._am_gallery_filter_form,
					'filter_form_name' : "GA-filter-by"
                                } );

        gallery.admin.am_gallery.set_row_fields (
                                [
                                        [ "id", "id" ],
					[ "Titolo", "title" ],
					[ "File name", "name" ],
					[ "Ext.", "ext" ],
					[ "Tipo", "kind" ],
					[ "Thumb", "_thumb" ],
					[ "created", "created" ]
                                ]
                                );

        gallery.admin.am_gallery.cbacks [ 'row_manip' ] = gallery.admin._rwman_gallery;
        gallery.admin.am_gallery.cbacks [ 'del_item' ]  = function ( id ) { gallery.admin.del ( id ); };
        gallery.admin.am_gallery.cbacks [ 'edit_item' ]  = function ( id ) { gallery.admin.edit ( id ); };
	gallery.admin.am_gallery.cbacks [ 'noresult' ] = function () { gallery.admin.add_item (); };
        gallery.admin.am_gallery.templates [ 'module_single_action' ] = gallery.templates [ 'module_single_action' ];

};

gallery.admin._rwman_gallery = function ( ds, row )
{
	row [ "_id" ] = row [ 'id' ];
	row [ "_thumb" ] = String.formatDict ( gallery.templates [ 'GA-image-thumb' ], { id : row [ 'id' ], width: 50 } );
};

gallery.admin._am_gallery_filter_form = function ( dest_div )
{       
        var f = new liwe.form.instance ( "GA-filter-by" );
        f.hidden ( "id_gal" , gallery.admin._cur_id_gal );
        f.set ( dest_div );
};


//}}}


//{{{ SHOCASE ADD EDIT SAVE FROM DELETE
gallery.admin.add_item = function ( id )
{
        $ ( 'title-work', 'Inserisci Nuova immgine alla gallery [ ' + gallery.admin._cur_gal_name + ' ]' );
	gallery.admin._create_add_form ( 0 );
};


gallery.admin._create_add_form = function ( id )
{
	var f = new liwe.form.instance ( 'GA-add-form' ); 

	f.events [ 'complete' ] = gallery.admin._refresh;
	f.hidden ( "action", 'gallery.admin.add_image' );
	f.hidden ( "__PYHP_SESSION", liwe.cookie.get ( "session" ) );
	f.hidden ( "iid", id );
	f.hidden ( "id_gal", gallery.admin._cur_id_gal );
	if ( id == 0 )	f.upload ( { label : 'Immagine', name: 'upload', mandatory : true } );
	f.text( { name: 'title', value: '' , label: 'Titolo' } );
	f.textarea ( { name: 'descr', value: '' , label: 'Descrizione', rows : 10 , cols : 39 } );
	f.submit ( { value: 'Salva', 'class' : 'ga-save-btn', nonl : true } );
        f.button ( { value: 'Annulla', onclick : "gallery.admin._refresh()", 'class' : 'ga-exit-btn' } );
	f.sep ();
	f.workspace ( { name: "GA-image" } );
	f.set ( 'ga-workspace' );

};


gallery.admin._refresh = function ()
{
        gallery.admin.am_gallery.show_list ( 'ga-workspace' );
};


gallery.admin.edit = function ( id )
{
	{
		gallery.admin.ajax ( { action : "gallery.admin.edit", iid : id }, function ( data )
					{
						gallery.admin._create_add_form ( id );
						var f = liwe.form.get ( "GA-add-form" );
						['iid', 'descr', 'title' ].iterate ( function ( v, k ) {
			                                n = data [ v ];
			                                if ( n === undefined ) n = 0;
                			                f.set_value ( v, n );
						} );

						data [ 'width' ] = 200;
						$( "GA-image" ).innerHTML = String.formatDict ( gallery.templates [ 'GA-image-thumb' ], data );
						return;
					} );
	}
};


gallery.admin.del = function ( id )
{
	var conf = false;
	conf = confirm ( "Vuoi eliminare l'elemento ?" );
	if ( conf )
	{
		gallery.admin.ajax ( { action : "gallery.admin.delete", ids : id }, function ( data )
					{
        					gallery.admin.am_gallery.show_list ( 'ga-workspace' );
					} );
	}
};

//}}}

//}}}
gallery.admin._jconed_form = function ( values )
{
	values = values || {};
	var vals = { 
			action : 'gallery.ajax.list_galleries',
			quant : 0
			};
	var opts = [];
	gallery.admin.ajax ( vals, function ( res )
	{
		if ( res.rows.count () < 1 ) return;

		res.rows.iterate ( function ( v, k ) 
		{
			opts.push ( { label: v.name, value : v.id } );			
		} );
		var f = new liwe.form.instance ( 'jconed-gallery' );

		f.hidden ( '_module', 'gallery' );
                f.hidden ( '_title', 'Gallery' );
		f.select ( { name: 'name', options: opts, mandatory: true, label: 'Gallery', value: values.get ( 'name' ) } );
		f.number ( { label: 'width', name: 'width', mandatory: true, value: values.get ( 'width' ) } );
		f.number ( { label: 'height', name: 'height', mandatory: true, value: values.get ( 'height' ) } );
		f.button ( { value: "Conferma", onclick: "jconed.render_plugin('jconed-gallery')" } );
		jconed.toolbar.set_context ( f.get () );
	} );
	
};

gallery.admin._jconed_handler = function ()
{
	gallery.admin._jconed_form ();
};

gallery.admin._jconed_context = function ()
{
        var item = jconed.get_plugin_selection ();
        if ( ! item ) return;
	if ( ! liwe.dom.has_class ( item, 'liwe-plugin-gallery' ) ) return;

        pre = item.getElementsByTagName ( "pre" );
        if ( ! pre.length ) return;

        pre = pre [ 0 ];

	var values = null;
        eval ( 'values = ' + pre.innerHTML );

	//if ( typeof ( values ) != 'object' ) return; 

	gallery.admin._jconed_form ( values [ 'params' ] );
};
